<template>
	<div></div>
</template>

<script>
import { queryPermissionMenu } from '@/api/system/tbSysMenu';
const urlReg = new RegExp('^((http|https):\/\/)?(([A-Za-z0-9]+-[A-Za-z0-9]+|[A-Za-z0-9]+)\.)+([A-Za-z]+)[/\?\:]?.*$');
export default {
	data() {
		return {
			menuData: []
		};
	},
	mounted() {
		this.getMenuData();
	},
	methods: {
		async getMenuData() {
			let result = await queryPermissionMenu().then((res) => res.data);
			if (result.code === '0') {
				let datas = this.$detailData(result.data);
				this.menuData = this.formatMenuData(datas.menu || []);
			}
		},

		//格式化菜单数据
		formatMenuData(arr) {
			let menus = [];
			arr.forEach((item) => {
				let obj = { id: item.id, name: item.name, url: item.url };
				obj['isLink'] = !!urlReg.test(item.url);
				obj['path'] = this.formatNodePath(item);
				if (!!item.childList && item.childList.length > 0) {
					obj['children'] = this.formatMenuData(item.childList);
				}
				menus.push(obj);
			});
			return menus;
		},

		//格式化节点路径
		formatNodePath(data) {
		},

		//格式化菜单路由
		formatMenuRouter(arr) {
			let menus = [];
			arr.forEach((item) => {
				let obj = { id: item.id };
				if (!urlReg.test(item.url)) {
					obj['path'] = this.formatRouterPath(item);
					obj['name'] = this.formatRouterName(item.id);
					obj['query'] = this.formatRouterQuery(item.url);
					obj['meta'] = { title: item.name };
				} else {
					obj['path'] = item.url;
				}
				if (!!item.childList && item.childList.length > 0) {
					obj['children'] = this.formatMenuRouter(item.childList);
				}
				menus.push(obj);
			});
			return menus;
		},

		//格式化路由路径
		formatRouterPath(data) {
			let path = data.url.split('?');
			let urlArr = path[0].split('/').filter((n) => !!n);
			if (urlArr.length === 1 && data.pid === '0') {
				return `/${urlArr[0]}`;
			} else {
				return urlArr[urlArr.length - 1];
			}
		},

		//格式化路由名称
		formatRouterName(id) {
			const findNodePath = (tree, id, path = []) => {
				for (const node of tree) {
					if (node.id === id) {
						return [...path, node.url]; // 找到节点时返回路径
					}
					if (node.childList) {
						const foundPath = findNodePath(node.childList, id, [...path, node.url]);
						if (foundPath) {
							return foundPath; // 递归找到路径时返回
						}
					}
				}
				return null; // 未找到节点时返回null
			};
			const capitalizeFirstLetter = (string) => {
				return string.toLowerCase().replace(/\b[a-z]/g, function (match) {
					return match.toUpperCase();
				});
			};
			let menuPaths = findNodePath(this.menuData, id).map((item) => {
				let path = item.split('?')[0];
				let paths = path.split('/');
				return capitalizeFirstLetter(paths[paths.length - 1]);
			});
			return menuPaths.join('');
		},

		//格式化路由参数
		formatRouterQuery(url) {
			let urlArr = url.split('?');
			if (!urlArr[1]) {
				return {};
			} else {
				let obj = {};
				urlArr[1].split('&').forEach((item) => {
					let itemArr = item.split('=');
					obj[itemArr[0]] = itemArr[1];
				});
				return obj;
			}
		}
	}
};
</script>

<style scoped></style>
